package com.xiaodou.mjf.web.xface.core.utils;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.xiaodou.mjf.infrastructure.common.utils.StringUtils;
import com.xiaodou.mjf.web.xface.common.API;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.lang3.ArrayUtils;

import javax.crypto.Cipher;
import java.security.Key;
import java.security.KeyFactory;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
import java.util.Map;

/**
 * Created by 文科 on 2015/3/11.
 */
public class Decrypt {

    public static String doDecrypt(String param){

        try {
            if(StringUtils.isNotBlank(param)){
                param = param.replace(" ","+");

                PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(Base64.decodeBase64(Constant.PRIVARY_KEY));
                KeyFactory keyFactory = KeyFactory.getInstance("RSA");

                Key privateKey = keyFactory.generatePrivate(keySpec);

                Cipher cipher = Cipher.getInstance("RSA");
                cipher.init(Cipher.DECRYPT_MODE,privateKey);

                byte[] data = Base64.decodeBase64(param);

                StringBuilder sb = new StringBuilder();
                for (int i = 0; i < data.length; i += 128) {
                    byte[] doFinal = cipher.doFinal(ArrayUtils.subarray(data, i,
                            i + 128));
                    sb.append(new String(doFinal));
                }
                return sb.toString();
            }
            return null;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }

    }

    public static String doEncrypt(Map<String,String> params){
        try {
            String PUBLIC_KEY = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDUBpzutmxJlr1sO7XraszmdRIaFJLAq8cOqZbd+ciOBytES1ZSkZwa0jGkSUP1SGynev2T+fTVEyc2IXr/vKEepuwY+6zI/3ir+px2nnc3vwhdhfw5+tKWdrNZr6HHTGYolYq6bKd4E5lb2hV1alkVbpNw7FM5ESZpZa8rChDEywIDAQAB";

            X509EncodedKeySpec keySpec = new X509EncodedKeySpec(Base64.decodeBase64(PUBLIC_KEY));
            KeyFactory factory = KeyFactory.getInstance("RSA");
            Key publicKey = factory.generatePublic(keySpec);
            Cipher cipher = Cipher.getInstance("RSA");
            cipher.init(Cipher.ENCRYPT_MODE,publicKey);

            byte[] data = new ObjectMapper().writeValueAsString(params).getBytes();

            byte[] buffer = null;

            for (int i = 0; i < data.length; i += 64) {
                byte[] doFinal = cipher.doFinal(ArrayUtils.subarray(data, i,
                        i + 64));
                buffer = ArrayUtils.addAll(buffer,doFinal);

            }

            String param = Base64.encodeBase64String(buffer);
            return param;


        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    public static String doDecrypt(String param,String priKey){

        try {
            if(StringUtils.isNotBlank(param)){
                param = param.replace(" ","+");

                PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(Base64.decodeBase64(priKey));
                KeyFactory keyFactory = KeyFactory.getInstance("RSA");

                Key privateKey = keyFactory.generatePrivate(keySpec);

                Cipher cipher = Cipher.getInstance("RSA");
                cipher.init(Cipher.DECRYPT_MODE,privateKey);

                byte[] data = Base64.decodeBase64(param);

                StringBuilder sb = new StringBuilder();
                for (int i = 0; i < data.length; i += 128) {
                    byte[] doFinal = cipher.doFinal(ArrayUtils.subarray(data, i,
                            i + 128));
                    sb.append(new String(doFinal));
                }
                return sb.toString();
            }
            return null;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }

    }
}
